/* BaseExportAction.java
 * Author: liuex
 * 2007-12-19 ����11:33:03
 *
 * Note: 
 */
package lex.proxyverfier.actions;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

import javax.swing.JFileChooser;

import org.lex.io.filesystem.FileNameUtilities;
import org.lex.net.proxy.ProxyServer;

import lex.proxyverfier.ProxyVerifier;

public abstract class BaseExportAction extends BaseImportExportAction
{
	protected abstract String getFilePostfix();

	protected abstract void export( Collection<ProxyServer> servers, File out ) throws IOException;

	protected void doInBackground( ActionEvent e )
	{
		JFileChooser fc = this.createFileChooser();
		Window root = ProxyVerifier.getApplication().getRootWindow();
		int opt = fc.showSaveDialog( root );
		if( JFileChooser.APPROVE_OPTION == opt )
		{
			File file = fc.getSelectedFile();
			File workingDirectory = file.getParentFile();
			this.setWorkingDirectory( workingDirectory );
			String path = file.getName();
			if( false == path.endsWith( this.getFilePostfix() ) )
			{
				file = new File( workingDirectory, FileNameUtilities.getBaseName( path )
						+ this.getFilePostfix() );
			}
			try
			{
				this.export( ProxyVerifier.getApplication().getProxyManager().getProxyServerList(),
					file );
			}
			catch( IOException e1 )
			{
				e1.printStackTrace();
				this.showError( "Error export file", e1.getMessage() );
			}
		}
	}
}
